package com.example.spd.service.taobaoUrl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.internal.util.StringUtils;
import com.taobao.api.request.*;
import com.taobao.api.response.*;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@Validated
public class TaobaoUrlServiceImpl implements TaobaoUrlService {
    //如有疑问请参考淘宝码上放心接口文档API:https://open.taobao.com/api.htm?docId=64956&docType=2&scopeId=20981
//    @Value("${taobao.select_drug_list_url}")
//    private String url = "https://gw.api.taobao.com/router/rest";
//    @Value("${taobao.appkey}")
//    private String appkey = "35029840";
//    @Value("${taobao.secret}")
//    private String secret = "7b61e10bd80604e404fe4fd0a90edd85";
//    @Value("${taobao.ent_id}")
//    private String entId = "1575e13fd22d4d848ccf758fe064e0c1";
//    @Value("${taobao.ref_ent_name}")
//    private String refEntName = "长春市九台区人民医院";
    @Value("${taobao.select_drug_list_url}")
    public String url1;
    @Value("${taobao.appkey}")
    private String appkey1;
    @Value("${taobao.secret}")
    private String secret1;
    @Value("${taobao.ent_id}")
    private String entId1;
    @Value("${taobao.ref_ent_name}")
    private String refEntName1;
    static String url;
    static String appkey;
    static String entId;
    static String secret;
    static String refEntName;
    @PostConstruct
    public void init() {
        url = url1;
        appkey = appkey1;
        entId = entId1;
        secret = secret1;
        refEntName = refEntName1;
        System.out.println("配置注入的url：" + url); // 若打印null，说明配置未注入
    }
    //    查询药品目录信息
    @Override
    public Object selectDrugListUrl(String code) {
        TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
        AlibabaAlihealthDrugtraceTopYljgQueryCodedetailRequest req = new AlibabaAlihealthDrugtraceTopYljgQueryCodedetailRequest();
        //企业唯一标识
        req.setRefEntId(entId);
        // 码列表【多个码用逗号拼接的字符串。要求数量在1000个码以下，但一般不要传这么多，如果网络不好很容易传输一半报错】
        //req.setCodes("87709840000000015538");
        //code 为追溯码信息
        req.setCodes(code);
        AlibabaAlihealthDrugtraceTopYljgQueryCodedetailResponse rsp = null;
        try {
            rsp = client.execute(req);
        } catch (ApiException e) {
            throw new RuntimeException(e);
        }
//        System.out.println("追溯:" + rsp.getBody());
        return rsp.getBody();
    }

    @Override
    public void uploadretail(String billCode, Date billTime, String billType, String traceCodes) {
        TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
        AlibabaAlihealthDrugtraceTopYljgUploadretailRequest req = new AlibabaAlihealthDrugtraceTopYljgUploadretailRequest();
        req.setBillCode(billCode);
        req.setBillTime(billTime);
        req.setBillType(Long.valueOf(billType));
        req.setPhysicType(3L);
        req.setRefUserId(entId);
        req.setTraceCodes(traceCodes);

//        req.setFromUserId("320000002332");
//        req.setOperIcCode("2100000345");
//        req.setOperIcName("张三");
//        req.setCustomerIdType("5");
//        req.setCustomerId("60123456789");
//        req.setUserTel("15800001111");
//        req.setNetworkBillFlag("1");
//        req.setMedicDoctor("李四");
//        req.setMedicDispenser("王五");
//        req.setUserName("刘巴");
//        req.setUserAgent("代理领药人");
//        req.setRemarks("备注信息");
        AlibabaAlihealthDrugtraceTopYljgUploadretailResponse rsp = null;
        try {
            rsp = client.execute(req);
        } catch (ApiException e) {
            throw new RuntimeException(e);
        }
        // 日志打印
//        System.out.println(rsp.getBody());
    }

    @Override
    public void traceCodes(String billCode, Date billTime, Integer billType, Integer physicType, String fromUserId, String toUserId, String traceCodes) {
        TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
        AlibabaAlihealthDrugtraceTopYljgUploadinoutbillRequest req = new AlibabaAlihealthDrugtraceTopYljgUploadinoutbillRequest();
        req.setBillCode(billCode);
        req.setBillTime(billTime);
        ;
        req.setBillType(Long.valueOf(billType));
        req.setPhysicType(3L);
        req.setFromUserId(fromUserId);
        req.setToUserId(fromUserId);
        req.setOperIcCode(traceCodes);
        req.setClientType("2");

//        req.setRefUserId("320000000000127971");
//        req.setAgentRefUserId("320000000000127971");
//        req.setDestUserId("5069452c34b94a778abaa26c2b40b305");
//        req.setOperIcName("张三");
//        req.setWarehouseId("W001");
//        req.setDrugId("D001");
//        req.setTraceCodes("81012350000000157474");
//        req.setReturnReasonCode("1");
//        req.setReturnReasonDes("退货原因描述");
//        req.setCancelReasonCode("1");
//        req.setCancelReasonDes("注销原因描述");
//        req.setExecuterName("执行人");
//        req.setExecuterCode("11034564321");
//        req.setSuperviserName("监督人");
//        req.setSuperviserCode("11276789342");
//        req.setFromAddress("发货地址XXX");
//        req.setToAddress("收货地址XXX");
//        req.setFromBillCode("123456");
//        req.setOrderCode("123456");
//        req.setFromPerson("张某");
//        req.setToPerson("李某");
//        req.setDisRefEntId("5069452c34b94a778abaa26c2b40b305");
//        req.setDisEntId("5069452c34b94a778abaa26c2b40b305");
//        req.setQuReceivable(10L);
//        req.setXtIsCheck("0");
//        req.setXtCheckCode("未验证通过原因");
//        req.setXtCheckCodeDesc("未通过原因描述");
//        req.setDrugListJson("[{\"codeCount\":100,\"commDrugId\":\"testCommDrugId0\",\"exprieDate\":1571131734945,\"physicInfo\":\"test0\",\"pkgSpec\":\"test0\",\"prepnCount\":10,\"produceBatchNo\":\"test0\",\"produceDate\":1571131734945},{\"codeCount\":100,\"commDrugId\":\"testCommDrugId1\",\"exprieDate\":1571131734945,\"physicInfo\":\"test1\",\"pkgSpec\":\"test1\",\"prepnCount\":10,\"produceBatchNo\":\"test1\",\"produceDate\":1571131734945}]");
//        req.setAssRefEntId("5069452c34b94a778abaa26c2b40b305");
//        req.setAssEntId("5069452c34b94a778abaa26c2b40b305");
        AlibabaAlihealthDrugtraceTopYljgUploadinoutbillResponse rsp = null;
        try {
            rsp = client.execute(req);
        } catch (ApiException e) {
            throw new RuntimeException(e);
        }
//        System.out.println(rsp.getBody());
    }

    @Override
    public Object billstatus(String beginDate, String endDate, String billType, Long pageSize, Long page) {
        TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
        AlibabaAlihealthDrugtraceTopYljgQueryBillstatusRequest req = new AlibabaAlihealthDrugtraceTopYljgQueryBillstatusRequest();
        req.setRefEntId(entId);
        req.setBeginDate(beginDate);
        req.setEndDate(endDate);
        req.setBillType(billType);
        req.setPageSize(pageSize);
        req.setPage(page);

//        req.setBillCode("SYS_IN_201811061737543754_0002");
//        req.setDrugType("1");
//        req.setDealStatus("1");
//        req.setFromUserId("320000000000127971");
//        req.setToUserId("320000000000127971");
//        req.setAgentRefUserId("320000000000127971");

        AlibabaAlihealthDrugtraceTopYljgQueryBillstatusResponse rsp = null;
        try {
            rsp = client.execute(req);
        } catch (ApiException e) {
            throw new RuntimeException(e);
        }
//        System.out.println(rsp.getBody());
        return rsp.getBody();

    }

    @Override
    public Object upbillcode(String code) {
        // 无论是否包含逗号，都进行批量处理
        String[] codes = code.contains(",") ? code.split(",") : new String[]{code};

        // 遍历处理所有code
        for (String singleCode : codes) {
            // 打印调用信息
            System.out.println(String.format("[%s] 调用 upbillcode 方法，参数: %s",
                    new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()),
                    singleCode));

            // 创建客户端和请求
            TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
            AlibabaAlihealthDrugtraceTopYljgQueryUpbillcodeRequest req = new AlibabaAlihealthDrugtraceTopYljgQueryUpbillcodeRequest();
            req.setCode(singleCode);
            req.setRefEntId(entId);

            try {
                // 执行请求
                AlibabaAlihealthDrugtraceTopYljgQueryUpbillcodeResponse rsp = client.execute(req);
                Object upbillcode = rsp.getBody();

                // 打印返回结果
                System.out.println(String.format("[%s] upbillcode 方法返回: %s",
                        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()),
                        upbillcode));

                // 解析响应
                String upbillcodeStr = upbillcode.toString();
                ObjectMapper objectMapper = new ObjectMapper();
                JsonNode rootNode = objectMapper.readTree(upbillcodeStr);

                // 获取fromUserId
                String fromUserId = rootNode.path("alibaba_alihealth_drugtrace_top_yljg_query_upbillcode_response")
                        .path("result")
                        .path("model_list")
                        .path("bill_upstream_d_t_o")
                        .get(0)
                        .path("from_user_id")
                        .asText();

                // 调用第二个方法
                this.uploadinoutbill(singleCode, fromUserId);

            } catch (ApiException | JsonProcessingException e) {
                System.out.println(String.format("[%s] 处理 code: %s 时发生异常: %s",
                        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()),
                        singleCode,
                        e.getMessage()));
                throw new RuntimeException(e);
            }
        }

        // 最后调用relation方法
        return this.relation(code);
    }
//    public Object upbillcode(String code) {
//        // 检查codes是否包含逗号，
//        if(code.contains(",")){
//            // 如果包含逗号拆分为多个code
//            String[] codes = code.split(",");
//            // 循环处理多个code
//            for(String singleCode : codes){
//                TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
//                AlibabaAlihealthDrugtraceTopYljgQueryUpbillcodeRequest req = new AlibabaAlihealthDrugtraceTopYljgQueryUpbillcodeRequest();
//                req.setCode(singleCode);
//                req.setRefEntId(entId);
//                AlibabaAlihealthDrugtraceTopYljgQueryUpbillcodeResponse rsp = null;
//                try {
//                    rsp = client.execute(req);
//                } catch (ApiException e) {
//                    throw new RuntimeException(e);
//                }
//                Object upbillcode = rsp.getBody();
//                String upbillcodeStr = upbillcode.toString();
//                ObjectMapper objectMapper = new ObjectMapper();
//                JsonNode rootNode = null;
//                try {
//                    rootNode = objectMapper.readTree(upbillcodeStr);
//                } catch (JsonProcessingException e) {
//                    throw new RuntimeException(e);
//                }
//                String fromUserId = rootNode.path("alibaba_alihealth_drugtrace_top_yljg_query_upbillcode_response")
//                        .path("result")
//                        .path("model_list")
//                        .path("bill_upstream_d_t_o")
//                        .get(0)
//                        .path("from_user_id")
//                        .asText();
//                // 使用fromUserId调用第二个方法
//
//
//                this.uploadinoutbill(singleCode, fromUserId);
//            }
//            // 不含有逗号说明扫描单码，实现单码查询
//        }else{
//            TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
//            AlibabaAlihealthDrugtraceTopYljgQueryUpbillcodeRequest req = new AlibabaAlihealthDrugtraceTopYljgQueryUpbillcodeRequest();
//            req.setCode(code);
//            req.setRefEntId(entId);
//            AlibabaAlihealthDrugtraceTopYljgQueryUpbillcodeResponse rsp = null;
//            try {
//                rsp = client.execute(req);
//            } catch (ApiException e) {
//                throw new RuntimeException(e);
//            }
////        System.out.println(rsp.getBody());
//            Object upbillcode = rsp.getBody();
//            String upbillcodeStr = upbillcode.toString();
//            ObjectMapper objectMapper = new ObjectMapper();
//            JsonNode rootNode = null;
//            try {
//                rootNode = objectMapper.readTree(upbillcodeStr);
//            } catch (JsonProcessingException e) {
//                throw new RuntimeException(e);
//            }
//            String fromUserId = rootNode.path("alibaba_alihealth_drugtrace_top_yljg_query_upbillcode_response")
//                    .path("result")
//                    .path("model_list")
//                    .path("bill_upstream_d_t_o")
//                    .get(0)
//                    .path("from_user_id")
//                    .asText();
//            // 使用fromUserId调用第二个方法
//        }
//
////        this.relation(code);
//        return this.relation(code);
//    }

    @Override
    public Object relation(String code) {
        TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
        AlibabaAlihealthDrugtraceTopYljgQueryRelationRequest req = new AlibabaAlihealthDrugtraceTopYljgQueryRelationRequest();
        req.setRefEntId(entId);
        req.setCode(code);
        req.setDesRefEntId(entId);
        AlibabaAlihealthDrugtraceTopYljgQueryRelationResponse rsp = null;
        try {
            rsp = client.execute(req);
        } catch (ApiException e) {
            throw new RuntimeException(e);
        }
        System.out.println("zuihzongjieguo\n\n\n" + rsp.getBody());
        return rsp.getBody();
    }

    @Override
    public Object getentinfo() {
        TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
        AlibabaAlihealthDrugtraceTopYljgQueryGetentinfoRequest req = new AlibabaAlihealthDrugtraceTopYljgQueryGetentinfoRequest();
        req.setEntName(refEntName);
        AlibabaAlihealthDrugtraceTopYljgQueryGetentinfoResponse rsp = null;
        try {
            rsp = client.execute(req);
        } catch (ApiException e) {
            throw new RuntimeException(e);
        }
//        System.out.println(rsp.getBody());
        return rsp.getBody();
    }

    @Override
    public Object codedetail(String codes) {
//        System.out.println("测试全局变量url路径"+url);
//        System.out.println("测试全局变量appkey路径"+appkey);
//        System.out.println("测试全局变量secret路径"+secret);
//        System.out.println("测试全局变量entId路径"+entId);
        TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
        AlibabaAlihealthDrugtraceTopYljgQueryCodedetailRequest req = new AlibabaAlihealthDrugtraceTopYljgQueryCodedetailRequest();
        req.setRefEntId(entId);
        req.setCodes(codes);
        AlibabaAlihealthDrugtraceTopYljgQueryCodedetailResponse rsp = null;
        try {
            rsp = client.execute(req);
        } catch (ApiException e) {
            throw new RuntimeException(e);
        }
//        System.out.println(rsp.getBody());
        return rsp.getBody();
    }

    @Override
    public Object uploadinoutbill(String codes) {
        TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
        AlibabaAlihealthDrugtraceTopYljgQueryUpbilldetailRequest req = new AlibabaAlihealthDrugtraceTopYljgQueryUpbilldetailRequest();
        req.setBillCode(codes);
        req.setRefEntId(entId);
        AlibabaAlihealthDrugtraceTopYljgQueryUpbilldetailResponse rsp = null;
        try {
            rsp = client.execute(req);
        } catch (ApiException e) {
            throw new RuntimeException(e);
        }
//        System.out.println(rsp.getBody());
        return rsp.getBody();
    }

    @Override
    public Object uploadinoutbill(String codes, String fromUserId) {
        TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
        AlibabaAlihealthDrugtraceTopYljgUploadinoutbillRequest req = new AlibabaAlihealthDrugtraceTopYljgUploadinoutbillRequest();
        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 获取当前时间并格式化
        String currentTime = LocalDateTime.now().format(formatter);
        req.setBillCode(UUID.randomUUID().toString().replace("-", "").substring(0, 12));
        // 设置到req对象中
        req.setBillTime(StringUtils.parseDateTime(currentTime));
        req.setBillType(102L);
        req.setPhysicType(3L);
        req.setRefUserId(entId);
//        req.setAgentRefUserId("320000000000127971");
        req.setFromUserId(fromUserId);
        req.setToUserId(entId);
        req.setClientType("2");
        req.setTraceCodes(codes);

//        System.out.println(UUID.randomUUID().toString().replace("-", "").substring(0, 12));
//        System.out.println(StringUtils.parseDateTime(currentTime));
        AlibabaAlihealthDrugtraceTopYljgUploadinoutbillResponse rsp = null;
        try {
            rsp = client.execute(req);
        } catch (ApiException e) {
            throw new RuntimeException(e);
        }
//        System.out.println(rsp.getBody());

        return rsp.getBody();
    }


    @Override
    public Object listupout(String beginDate, String endDate, Long pageSize, Long page) {
        TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
        AlibabaAlihealthDrugtraceTopYljgListupoutRequest req = new AlibabaAlihealthDrugtraceTopYljgListupoutRequest();
        req.setRefEntId(entId);
        req.setBeginDate(beginDate);
        req.setEndDate(endDate);
        req.setPageSize(pageSize);
        req.setPage(page);
//        req.setFromUserId("a3d6b0f3d6994bacb6055d3e00b2e4bc");
//        req.setProduceBatchNo("生产批号0702-01");
//        req.setDrugEntBaseInfoId("20b29f58513a48a8a29f04169372d3f5");
//        req.setBillType("201");
//        req.setPhysicType("3");
//        req.setStatus("1");
//        req.setBillCode("销售出库-0704");
        AlibabaAlihealthDrugtraceTopYljgListupoutResponse rsp = null;
        try {
            rsp = client.execute(req);
        } catch (ApiException e) {
            throw new RuntimeException(e);
        }
//        System.out.println(rsp.getBody());
        return rsp.getBody();
    }

    @Override
    public Object detail(String billCode, String fromRefUserId, String toRefUserId) {
        TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
        AlibabaAlihealthDrugtraceTopYljgListupoutDetailRequest req = new AlibabaAlihealthDrugtraceTopYljgListupoutDetailRequest();
        req.setRefEntId(entId);
        req.setBillCode(billCode);
        req.setFromRefUserId(fromRefUserId);
        req.setToRefUserId(toRefUserId);
        AlibabaAlihealthDrugtraceTopYljgListupoutDetailResponse rsp = null;
        try {
            rsp = client.execute(req);
        } catch (ApiException e) {
            throw new RuntimeException(e);
        }
//        System.out.println(rsp.getBody());
        return rsp.getBody();
    }

    @Override
    public Object listparts(String pageSize, String page) {
        TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
        AlibabaAlihealthDrugtraceTopYljgQueryListpartsRequest req = new AlibabaAlihealthDrugtraceTopYljgQueryListpartsRequest();
        req.setRefEntId(entId);
        req.setPageSize(Long.valueOf(pageSize));
        req.setPage(Long.valueOf(page));

//        req.setEntName("名称");
//        req.setRefPartnerId("123001129");
//        req.setBeginDate("2018-12-26 13:55:00");
//        req.setEndDate("2018-12-26 13:55:00");
        AlibabaAlihealthDrugtraceTopYljgQueryListpartsResponse rsp = null;
        try {
            rsp = client.execute(req);
        } catch (ApiException e) {
            throw new RuntimeException(e);
        }
//        System.out.println(rsp.getBody());
        return rsp.getBody();
    }

    @Override
    public Object getbyrefentid(String destRefEntId) {
        TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
        AlibabaAlihealthDrugtraceTopYljgQueryGetbyrefentidRequest req = new AlibabaAlihealthDrugtraceTopYljgQueryGetbyrefentidRequest();
        req.setRefEntId(entId);
        req.setDestRefEntId(destRefEntId);
        AlibabaAlihealthDrugtraceTopYljgQueryGetbyrefentidResponse rsp = null;
        try {
            rsp = client.execute(req);
        } catch (ApiException e) {
            throw new RuntimeException(e);
        }
//        System.out.println(rsp.getBody());
        return rsp.getBody();
    }

    @Override
    public Object drugtable(String physicName, Long pageSize, Long page) {
        TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
        AlibabaAlihealthDrugtraceTopYljgDrugtableRequest req = new AlibabaAlihealthDrugtraceTopYljgDrugtableRequest();
        req.setRefEntId(entId);
        req.setPhysicName(physicName);
        req.setPageSize(pageSize);
        req.setPage(page);
//        req.setApprovalLicenceNo("批准文号");
//        req.setStartDate("2018-11-19 17:50:23");
//        req.setEndDate("2018-11-19 17:50:23");
//        req.setEntName("测试");
//        req.setPackageSpec("1");
//        req.setPrepnSpec("1");
        AlibabaAlihealthDrugtraceTopYljgDrugtableResponse rsp = null;
        try {
            rsp = client.execute(req);
        } catch (ApiException e) {
            throw new RuntimeException(e);
        }
//        System.out.println(rsp.getBody());
        return rsp.getBody();
    }

    @Override
    public Object getdruglist(String physicName, Long pageSize, Long page) {
        TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
        AlibabaAlihealthDrugtraceTopYljgGetdruglistRequest req = new AlibabaAlihealthDrugtraceTopYljgGetdruglistRequest();
        req.setRefEntId(entId);
        req.setPhysicName(physicName);
        req.setPageSize(pageSize);
        req.setPage(page);
//        req.setApprovalLicenceNo("批准文号");
//        req.setPackageSpec("10");
//        req.setPrepnSpec("1");
        AlibabaAlihealthDrugtraceTopYljgGetdruglistResponse rsp = null;
        try {
            rsp = client.execute(req);
        } catch (ApiException e) {
            throw new RuntimeException(e);
        }
//        System.out.println(rsp.getBody());
        return rsp.getBody();
    }

    @Override
    public Object getenddate() {
        TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
        AlibabaAlihealthDrugtraceTopYljgServiceGetenddateRequest req = new AlibabaAlihealthDrugtraceTopYljgServiceGetenddateRequest();
        req.setRefEntId(entId);
        req.setBusiness(1L);
        req.setService(11L);
        AlibabaAlihealthDrugtraceTopYljgServiceGetenddateResponse rsp = null;
        try {
            rsp = client.execute(req);
        } catch (ApiException e) {
            throw new RuntimeException(e);
        }
//        System.out.println(rsp.getBody());
        return rsp.getBody();
    }

    @Override
    public Object downloadurl() {
        TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
        AlibabaAlihealthDrugtraceTopYljgGetkeyflagdruginfoDownloadurlRequest req = new AlibabaAlihealthDrugtraceTopYljgGetkeyflagdruginfoDownloadurlRequest();
        req.setRefEntId(entId);
        AlibabaAlihealthDrugtraceTopYljgGetkeyflagdruginfoDownloadurlResponse rsp = null;
        try {
            rsp = client.execute(req);
        } catch (ApiException e) {
            throw new RuntimeException(e);
        }
//        System.out.println(rsp.getBody());
        return rsp.getBody();
    }
}